//
//  WCBComposingCompleteViewController.m
//  WeChatBuniess
//
//  Created by Guo Rui on 2017/12/15.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "WCBComposingCompleteViewController.h"
#import "UIImageView+WCAWebCache.h"
#import <SafariServices/SafariServices.h>
#import "YYModel.h"
#import "WCBDataHelper.h"
#import "WCAWeChatTimelineModel.h"
#import "WCBShaixuViewController.h"
#import "WCBAddressListTableViewController.h"

@interface WCBComposingCompleteViewController ()
@property (strong, nonatomic) UIView *topView;
@property (strong, nonatomic) UIView *bookDisplayView;

@property (strong, nonatomic) UIButton *shaixuButton;
@property (strong, nonatomic) UIButton *commitButton;

@property (strong, nonatomic) UILabel *dateLabel;
@end

@implementation WCBComposingCompleteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.95 alpha:1.00];
    self.title = @"排版完成";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
//    self.bookModel = [[WCBWeChatBookModel alloc] init];
//    self.bookModel.coverImage = @"http://pic40.nipic.com/20140425/18574481_112331407143_2.jpg";
//    self.bookModel.title = @"王小蒙的微信书";
//    self.bookModel.bookCoverInfo = @"32开软面";
//    self.bookModel.bookPageInfo = @"铜板纸内页";
//    self.bookModel.pageSize = @258;
//    self.bookModel.price = @256.00;
//    self.bookModel.startDate = @1234567890123;
//    self.bookModel.endDate = @2345678901234;
    
    
    [self topView];
    [self bookDisplayView];
    [self shaixuButton];
    [self commitButton];
}

#pragma mark - Private

#pragma mark - Actions
- (void)previewDidClick
{
    NSString *url = [NSString stringWithFormat:@"http://m.timeface.cn/podview?id=%@&type=2", self.bookModel.bookId];
    SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString:url]];
    [self presentViewController:safariViewController animated:YES completion:nil];
}

- (void)shaixuDidClick
{
    WCBShaixuViewController *vc = [[WCBShaixuViewController alloc] init];
    vc.bookId = self.bookModel.bookId;
    [self.navigationController pushViewController:vc animated:YES];
    
    __weak WCBComposingCompleteViewController *_self = self;
    vc.completion = ^(WCBWeChatBookModel *bookModel) {
        _self.bookModel = bookModel;
        [_self setupBookDisplaySubViews];
//        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
//        fmt.dateFormat = @"yyyy.MM";
//
//        NSString *startDesc = [fmt stringFromDate:[NSDate dateWithTimeIntervalSince1970:bookModel.startDate.floatValue / 1000]];
//        NSString *endDesc = [fmt stringFromDate:[NSDate dateWithTimeIntervalSince1970:bookModel.endDate.floatValue / 1000]];
//        NSString *timeRange = [NSString stringWithFormat:@"成书时间: %@ ~ %@", startDesc, endDesc];
//        _self.dateLabel.text = timeRange;
        
    };
}

- (void)commitDidClick
{
    WCBAddressListTableViewController *vc = [[WCBAddressListTableViewController alloc] init];
    vc.bookModel = self.bookModel;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Getter
- (UIButton *)shaixuButton
{
    if (!_shaixuButton) {
        _shaixuButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _shaixuButton.backgroundColor = [UIColor whiteColor];
        [_shaixuButton setTitleColor:[UIColor colorWithRed:0.11 green:0.62 blue:0.94 alpha:1.00] forState:UIControlStateNormal];
        [_shaixuButton setTitle:@"筛选内容" forState:UIControlStateNormal];
        _shaixuButton.titleLabel.font = [UIFont systemFontOfSize:23];
        
        _shaixuButton.layer.cornerRadius = 5.f;
        _shaixuButton.layer.masksToBounds = YES;
        _shaixuButton.layer.borderColor = [UIColor colorWithRed:0.11 green:0.62 blue:0.94 alpha:1.00].CGColor;
        _shaixuButton.layer.borderWidth = 1.5f;
        
        [_shaixuButton addTarget:self action:@selector(shaixuDidClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_shaixuButton];
        
        CGFloat x, y, w, h;
        CGFloat width = CGRectGetWidth(self.view.frame);
        
        x = 30.f;
        w = (width - 3 * 30) / 2.f;
        y = CGRectGetMaxY(self.bookDisplayView.frame) + 50.f;
        h = 60.f;
        _shaixuButton.frame = CGRectMake(x, y, w, h);
    }
    return _shaixuButton;
}

- (UIButton *)commitButton
{
    if (!_commitButton) {
        _commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _commitButton.backgroundColor = [UIColor colorWithRed:0.11 green:0.62 blue:0.94 alpha:1.00];
        [_commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_commitButton setTitle:@"提交印刷" forState:UIControlStateNormal];
        _commitButton.titleLabel.font = [UIFont systemFontOfSize:23];
        
        _commitButton.layer.cornerRadius = 5.f;
        _commitButton.layer.masksToBounds = YES;
        
        [_commitButton addTarget:self action:@selector(commitDidClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_commitButton];
        
        CGFloat x, y, w, h;
        CGFloat width = CGRectGetWidth(self.view.frame);
        
        w = (width - 3 * 30) / 2.f;
        x = 30.f + w + 30.f;
        y = CGRectGetMaxY(self.bookDisplayView.frame) + 50.f;
        h = 60.f;
        _commitButton.frame = CGRectMake(x, y, w, h);
    }
    return _commitButton;
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.backgroundColor = [UIColor whiteColor];
        
        CGFloat x, y, w, h, maxY;
        CGFloat width = CGRectGetWidth(self.view.frame);
        
        {
            UILabel *label = [[UILabel alloc] init];
            label.textColor = [UIColor blackColor];
            label.font = [UIFont boldSystemFontOfSize:25];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = @"恭喜您!";
            [_topView addSubview:label];
            
            w = 120;
            x = (width - w) / 2.f;
            y = 40.f;
            h = 35;
            label.frame = CGRectMake(x, y, w, h);
            
            maxY = y + h;
        }
        
        {
            UILabel *label = [[UILabel alloc] init];
            label.textColor = [UIColor darkGrayColor];
            label.font = [UIFont systemFontOfSize:22];
            label.textAlignment = NSTextAlignmentCenter;
            label.text = [NSString stringWithFormat:@"您制作的《%@》已经排版完成!", self.bookModel.title];
            [_topView addSubview:label];
            
            x = 30;
            w = width - 2 * x;
            y = maxY + 20.f;
            h = 35;
            label.frame = CGRectMake(x, y, w, h);
            
            maxY = y + h;
        }
        
        {
            x = 0;
            y = 0;
            w = width;
            h = maxY + 40.f;
            _topView.frame = CGRectMake(x, y, w, h);
        }
        
        [self.view addSubview:_topView];
    }
    return _topView;
}

- (UIView *)bookDisplayView
{
    if (!_bookDisplayView) {
        _bookDisplayView = [[UIView alloc] init];
        _bookDisplayView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_bookDisplayView];
        
        [self setupBookDisplaySubViews];
    }
    return _bookDisplayView;
}

- (void)setupBookDisplaySubViews
{
    if (_bookDisplayView.subviews.count > 0) {
        [_bookDisplayView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    CGFloat x, y, w, h, maxX, maxY;
    CGFloat width = CGRectGetWidth(self.view.frame);
    {
        UIImageView *bookCoverView = [[UIImageView alloc] init];
        bookCoverView.userInteractionEnabled = YES;
        [bookCoverView setImageURL:[NSString stringWithFormat:@"%@@600w_600h_1l_1o", self.bookModel.coverImage] placeholder:nil];
        bookCoverView.layer.cornerRadius = 10;
        bookCoverView.layer.masksToBounds = YES;
        [_bookDisplayView addSubview:bookCoverView];
        
        UIImageView *bookSpineView = [[UIImageView alloc] init];
        bookSpineView.userInteractionEnabled = YES;
        bookSpineView.image = [UIImage imageNamed:@"TimeBookShadow@2x"];
        [bookCoverView addSubview:bookSpineView];
        
        UIView *maskView = [[UIView alloc] init];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0.6;
        [bookCoverView addSubview:maskView];
        
        UIButton *previewButtton = [UIButton buttonWithType:UIButtonTypeCustom];
        [previewButtton setTitle:@"点击预览" forState:UIControlStateNormal];
        [previewButtton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        previewButtton.titleLabel.font = [UIFont systemFontOfSize:23];
        
        previewButtton.layer.cornerRadius = 35.f;
        previewButtton.layer.masksToBounds = YES;
        previewButtton.layer.borderColor = [UIColor whiteColor].CGColor;
        previewButtton.layer.borderWidth = 1.5f;
        
        [previewButtton addTarget:self action:@selector(previewDidClick) forControlEvents:UIControlEventTouchUpInside];
        [bookCoverView addSubview:previewButtton];
        
        CGFloat bookCover_w = 235;
        CGFloat bookCover_h = 330;
        {
            x = 40;
            y = 30;
            w = bookCover_w;
            h = bookCover_h;
            bookCoverView.frame = CGRectMake(x, y, w, h);
            
            maxX = x + w;
            maxY = y + h;
        }
        
        {
            x = 0;
            y = 0;
            w = bookCover_w;
            h = bookCover_h;
            bookSpineView.frame = CGRectMake(x, y, w, h);
        }
        
        {
            x = 0;
            y = 0;
            w = bookCover_w;
            h = bookCover_h;
            maskView.frame = CGRectMake(x, y, w, h);
        }
        
        {
            x = 20;
            w = bookCover_w - 2 * x;
            h = 70;
            y = (bookCover_h - h) / 2.f;
            previewButtton.frame = CGRectMake(x, y, w, h);
        }
        
        {
            // bookDisplayView
            x = 0;
            y = CGRectGetMaxY(self.topView.frame) + 20.f;
            w = width;
            h = maxY + 30.f;
            _bookDisplayView.frame = CGRectMake(x, y, w, h);
        }
    }
    
    CGFloat label_h = 40.f;
    NSInteger count = 6;
    CGFloat margin  = ((maxY - 30) - (count * label_h)) / (count - 1);
    
    {
        // 书名
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:24];
        label.text = [NSString stringWithFormat:@"《%@》", self.bookModel.title];
        [_bookDisplayView addSubview:label];
        
        x = maxX + 30.f;
        y = 30.f;
        w = width - x - 30.f;
        h = label_h;
        label.frame = CGRectMake(x, y, w, h);
    }
    
    CGFloat offst = 9.5f;
    {
        // 封面
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:23];
        label.text = [NSString stringWithFormat:@"封面: %@", self.bookModel.bookCoverInfo];
        [_bookDisplayView addSubview:label];
        
        x = maxX + 30.f + offst;
        y = 30 + 1 * (label_h + margin);
        w = width - x - 30.f;
        h = label_h;
        label.frame = CGRectMake(x, y, w, h);
    }
    
    {
        // 内页
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:23];
        label.text = [NSString stringWithFormat:@"内页: %@", self.bookModel.bookPageInfo];
        [_bookDisplayView addSubview:label];
        
        x = maxX + 30.f + offst;
        y = 30 + 2 * (label_h + margin);
        w = width - x - 30.f;
        h = label_h;
        label.frame = CGRectMake(x, y, w, h);
    }
    
    {
        // 页数
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:23];
        label.text = [NSString stringWithFormat:@"页数: %@", self.bookModel.pageSize];
        [_bookDisplayView addSubview:label];
        
        x = maxX + 30.f + offst;
        y = 30 + 3 * (label_h + margin);
        w = width - x - 30.f;
        h = label_h;
        label.frame = CGRectMake(x, y, w, h);
    }
    
    {
        // 价格
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont systemFontOfSize:23];
        label.text = [NSString stringWithFormat:@"价格: ¥%.2lf", self.bookModel.price.floatValue];
        [_bookDisplayView addSubview:label];
        
        x = maxX + 30.f + offst;
        y = 30 + 4 * (label_h + margin);
        w = width - x - 30.f;
        h = label_h;
        label.frame = CGRectMake(x, y, w, h);
    }
    
    {
        // 成书时间
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        fmt.dateFormat = @"yyyy.MM";
        
        NSString *startDesc = [fmt stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.bookModel.startDate.floatValue / 1000]];
        NSString *endDesc = [fmt stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.bookModel.endDate.floatValue / 1000]];
        NSString *timeRange = [NSString stringWithFormat:@"%@ ~ %@", startDesc, endDesc];
        
        self.dateLabel = [[UILabel alloc] init];
        self.dateLabel.textColor = [UIColor darkGrayColor];
        self.dateLabel.font = [UIFont systemFontOfSize:23];
        self.dateLabel.text = [NSString stringWithFormat:@"成书时间: %@", timeRange];
        [_bookDisplayView addSubview:self.dateLabel];
        
        x = maxX + 30.f + offst;
        y = 30 + 5 * (label_h + margin);
        w = width - x - 30.f;
        h = label_h;
        self.dateLabel.frame = CGRectMake(x, y, w, h);
    }
}

@end
